<?php


namespace App\Http\Library;


use App\Config\CacheConfig;
use Illuminate\Support\Facades\Cache;

class Zws
{

    /**
     * EHR组织架构信息
     *
     * @param int $page
     * @param     $timeStart
     * @param int $perPage
     *
     * @return array
     */
    public static function mainDeptList($page = 1, $timeStart, $perPage = 1000)
    {
        /*获取ticket*/
        $ticket = self::getTicket(config('ext.zws_appid'), config('ext.zws_appkey'));
        /*请求参数*/
        $param = [
            'page'       => $page,
            'per_page'   => $perPage,
            'ticket'     => $ticket,
            /*查询UPDATE_TIME字段，运算符为大于等于，示例2019-09-02 14:13:58*/
            'time_start' => $timeStart
        ];
        $url = config('ext.zws_host') . '/cmpts/data/main/dept/list';
        Curl::$url = $url;
        Curl::$urlParam = $param;
        $response = Curl::commit();
        $result = json_decode($response, true);
        if (isset($result['errmsg']) && $result['errmsg'] === 'success') {
            return $result['data']['list'];
        }
        log_arr([
            '[EHR组织架构-异常]',
            'url'      => $url,
            'param'    => $param,
            'response' => $response,
            'result'   => $result
        ], 'zws');
        return [];
    }


    /**
     * EHR员工常用基本信息
     *
     * @param int      $page      页数
     * @param datetime $timeStart 查询UPDATE_TIME字段，运算符为大于等于，示例2019-09-02 14:13:58*
     * @param int      $per_page  每页条数
     *
     * @return array
     *
     */
    public static function employeeJobList($page = 1, $timeStart, $perPage = 1000)
    {
        /*获取ticket*/
        $ticket = self::getTicket(config('ext.zws_appid'), config('ext.zws_appkey'));
        /*请求参数*/
        $param = [
            'page'       => $page,
            'per_page'   => $perPage,
            'ticket'     => $ticket,
            /*查询UPDATE_TIME字段，运算符为大于等于，示例2019-09-02 14:13:58*/
            'time_start' => $timeStart
        ];
        $url = config('ext.zws_host') . '/cmpts/data/main/employee/job/list';
        Curl::$url = $url;
        Curl::$urlParam = $param;
        $response = Curl::commit();
        $result = json_decode($response, true);
        if (isset($result['errmsg']) && $result['errmsg'] === 'success') {
            return $result['data']['list'];
        }
        log_arr([
            '[EHR员工常用基本信息-异常]',
            'url'      => $url,
            'param'    => $param,
            'response' => $response,
            'result'   => $result
        ], 'zws');
        return [];
    }


    /**
     * SSO-身份认证
     * http://doc-openapi.zhiyinlou.com/docs/zws/sso/verify
     *
     * @param $token
     *
     * @return mixed
     */
    public static function verify($token)
    {
        /*获取ticket*/
        $ticket = self::getTicket(config('ext.zws_appid'), config('ext.zws_appkey'));
        /*调用身份认证接口*/
        $url = config('ext.zws_host') . '/api/v1/sso/verify';
        Curl::$url = $url;
        $param = [
            'token'  => $token,
            'ticket' => $ticket
        ];
        Curl::$urlParam = $param;
        $response = Curl::commit();
        $result = json_decode($response, true);
        if (!empty($result['errmsg']) && $result['errmsg'] == 'success') {
            return $result['data'];
        }
        log_arr([
            '[身份认证-异常]',
            'url'   => $url,
            'param' => $param,
            'res'   => $response
        ], 'zws');
        return null;
    }


    /**
     * ticket获取
     *
     * @param string $appId
     * @param string $appKey
     *
     * @return mixed
     */
    private static function getTicket(string $appId, string $appKey)
    {
        $key = CacheConfig::ZWS_APP_TICKET;
        return Cache::remember("{$key}:{$appId}", 6000, function() use ($appId, $appKey) {
            Curl::$url = config('ext.zws_host') . '/basic/get_ticket';
            Curl::$urlParam = [
                'appid'  => $appId,
                'appkey' => $appKey
            ];
            $response = Curl::commit();
            $result = json_decode($response, true);
            if (isset($result['errcode']) && $result['errcode'] === 0) {
                return $result['ticket'];
            } else {
                /*记录异常*/
                log_arr([
                    '[获取ticket失败]',
                    'line:' . __LINE__,
                    'file:' . __FILE__,
                    'response' => $response
                ], 'zws');
                return null;
            }
        });
    }

}





/**
 * Zws.php
 *
 * 说明:
 *
 * 修改历史
 * ----------------------------------------
 * 2021/3/10 19:06:00   操作:创建
 **/
